<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<%@include file="/header.jsp" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>权限分配页面</title>
<script type="text/javascript">
  function refGrid(){
	  $("#userTree").tree({url:"${base}sysPermission/orgList?roleId="+$("#roleId").val(),animate:true,checkbox:true,cascadeCheck:true});
	  $("#menuTree").tree({url:"${base}sysPermission/menuList?roleId="+$("#roleId").val(),animate:true,checkbox:true,cascadeCheck:true});
	  $("#pageTree").tree({url:"${base}sysPermission/pageList?roleId="+$("#roleId").val(),animate:true,checkbox:true,cascadeCheck:true});
	  }
  function saveUserPermission(){
	  var nodes=$("#userTree").tree("getChecked",['checked','indeterminate']);
	  ids=[];
	  $.each(nodes,function(index){
		 ids.push(nodes[index].id);
		  });
	  if(!ids){
		  myAlert("错误信息","请选择需要分配的用户！","e"); 
		}else{
			myConfirm("提示信息","确定为选择用户分配此角色吗?",function(r){
                if(r){
               	 myAjax({url:"${base}sysPermission/updatePermission",data:{"ids":ids,"roleId":$("#roleId").val(),"type":'user'},  
                   	 success:function(data){
                             if(data.error=='0')
                              myAlert("提示信息",data.msg,"i");      
                             else
                           	  myAlert("错误信息",data.msg,"e");   
                   	 }}); 
             }
		});
	}
  }
  function refUserPermission(){
	  $("#userTree").tree({url:"${base}sysPermission/orgList?roleId="+$("#roleId").val(),animate:true,checkbox:true,cascadeCheck:true});
  }
  function saveMenuPermission(){
	  var nodes=$("#menuTree").tree("getChecked",['checked','indeterminate']);
	  ids=[];
	  $.each(nodes,function(index){
		 ids.push(nodes[index].id);
		  });
	  if(!ids){
		  myAlert("错误信息","请选择需要分配的菜单！","e"); 
		}else{
			myConfirm("提示信息","确定分配菜单权限吗?",function(r){
                if(r){
               	 myAjax({url:"${base}sysPermission/updatePermission",data:{"ids":ids,"roleId":$("#roleId").val(),"type":'menu'},  
                   	 success:function(data){
                             if(data.error=='0')
                              myAlert("提示信息",data.msg,"i");      
                             else
                           	  myAlert("错误信息",data.msg,"e");   
                   	 }}); 
             }
		});
			}
	  }
  function refMenuPermission(){
	  $("#menuTree").tree({url:"${base}sysPermission/menuList?roleId="+$("#roleId").val(),animate:true,checkbox:true,cascadeCheck:true});
	  }
  function savePagePermission(){
	  var nodes=$("#pageTree").tree("getChecked",['checked','indeterminate']);
	  ids=[];
	  $.each(nodes,function(index){
		 ids.push(nodes[index].id);
		  });
	  if(!ids){
		  myAlert("错误信息","请选择需要分配的页面权限！","e"); 
		}else{
			myConfirm("提示信息","确定分配页面权限吗?",function(r){
                if(r){
               	 myAjax({url:"${base}sysPermission/updatePermission",data:{"ids":ids,"roleId":$("#roleId").val(),"type":'page'},  
                   	 success:function(data){
                             if(data.error=='0')
                              myAlert("提示信息",data.msg,"i");      
                             else
                           	  myAlert("错误信息",data.msg,"e");   
                   	 }}); 
             }
		});
			}
  }
function refPagePermission(){
	$("#pageTree").tree({url:"${base}sysPermission/pageList?roleId="+$("#roleId").val(),animate:true,checkbox:true,cascadeCheck:true});
  }
  $(function(){
	  $('#dl').datalist({
		    url: '${base}sysPermission/roleList',
		    queryParams:{roleName:$("#roleSearch").textbox('getValue')},
		    lines: true,
		    onSelect:function(rowIndex, rowData){
                $("#roleId").val(rowData.value);
                refGrid();
	  },onLoadSuccess:function(data){
		//默认选中第一条
		  if(data&&data.rows){
			  $('#dl').datalist("selectRow",0);
			  refGrid();
		  }
	  }
	});
	   
	  $("#roleSearch").textbox({onClickButton:function(){
		$('#dl').datalist("clearChecked");
		  var sText=$("#roleSearch").textbox('getValue');
		  $('#dl').datalist({url: '${base}sysPermission/roleList',queryParams:{roleName:sText}});
	}}); 
		  });
</script>
</head>
<body fit="true">
<div class="easyui-layout" fit="true">
		<div data-options="region:'west',split:false,border:false,title:'角色列表'"   style="width:200px;">
		       <table style="height:100%;width:100%;" cellpadding="0" cellspacing="1">
		       <tr height="30">
		       <td valign="middle">
		       <input id="roleId" type="hidden" />
		       <input id="roleSearch" class="easyui-textbox" 
		       data-options="buttonText:'',buttonIcon:'icon-search',prompt:'角色搜索'" 
		       style="width:198px;height:24px;"/>
                </td></tr>
		       <tr><td>
		       <div id="dl" fit="true"></div>
            </td></tr>
		       </table>
		</div>
		<div data-options="region:'center',border:false">
		 <table   cellspacing="1"  cellpadding="0"   style="width:100%;height:100%;">
		 <tr>
		 <td width="33%"><div class="easyui-panel" data-options="title:'用户列表',footer:'#userTool'" fit="true">
		   <ul id="userTree" fit="true"></ul>
		   </div></td>
		 <td width="33%"><div class="easyui-panel" data-options="title:'菜单列表',footer:'#menuTool'" fit="true">
		   <ul id="menuTree" fit="true"></ul>
		   </div></td>
		  <td width="33%"><div class="easyui-panel" data-options="title:'权限列表',footer:'#pageTool'"  fit="true">
		  <ul id="pageTree"  fit="true"></ul>
		   </div></td>
		   </tr>
		 </table>
		</div>
	</div>
	<div id="userTool" style="padding:5px;text-align:center;">
	    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="width:60px" onclick="saveUserPermission()">分配</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-refresh'" style="width:60px" onclick="refUserPermission()">刷新</a>
	</div>
	<div id="menuTool" style="padding:5px;text-align:center;">
	    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="width:60px" onclick="saveMenuPermission()">分配</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-refresh'" style="width:60px" onclick="refMenuPermission()">刷新</a>
	</div>
	<div id="pageTool" style="padding:5px;text-align:center;">
	    <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" style="width:60px;" id="saveBtn" onclick="savePagePermission()">分配</a>
		<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-refresh'" style="width:60px" id="cancelBtn" onclick="refPagePermission()">刷新</a>
	</div>
</body>
</html>